package com.pay.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.pay.constants.ExpriseTimeConstants;
import com.pay.constants.RedisKeyConstants;
import com.pay.entity.wx.MenuInfo;
import com.pay.mapper.wx.MenuInfoMapper;
import com.pay.service.MenuInfoService;
import com.pay.util.RedisUtil;

@Service("menuInfoService")
public class MenuInfoServiceImpl implements MenuInfoService {

	@Resource
	private MenuInfoMapper menuInfoMapper;

	@Override
	public MenuInfo findMenuInfoByMenuNo(String menuno) {
		String key = RedisKeyConstants.MENU_INFO_BEAN_MENUNO + menuno;
		String value = RedisUtil.getValue(key);
		MenuInfo menuInfo = null;
		// 不为空，直接转换值，否则查询数据库
		if (StringUtils.isNotBlank(value)) {
			menuInfo = JSON.parseObject(value, MenuInfo.class);
		} else {
			menuInfo = menuInfoMapper.findMenuInfoByMenuNo(menuno);
			// 重新将分享信息放入缓存中
			if (menuInfo != null) {
				RedisUtil.setKeyValue(key, JSON.toJSONString(menuInfo), ExpriseTimeConstants.expriseTime2592000);
			}
		}
		return menuInfo;
	}

}
